//
//  DHProductDetail_DianpingCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/11.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHProductDetail_DianpingCell.h"
#import "DHHomeComments_Cell.h"
#import "DHArticleNO_CommentCell.h"
#import "DHDianpingListController.h"
#import "DHNewsDetailController.h"

@interface DHProductDetail_DianpingCell()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *itemArr;

@end

@implementation DHProductDetail_DianpingCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHHomeComments_Cell" bundle:nil] forCellReuseIdentifier:kDHHomeComments_Cell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHArticleNO_CommentCell" bundle:nil] forCellReuseIdentifier:kDHArticleNO_CommentCell];
}

- (NSArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSArray array];
    }
    return _itemArr;
}

- (void)updateCellWithArray:(NSArray *)arr{
    self.itemArr = arr;
    [self.tableView reloadData];
}

- (IBAction)allAction:(id)sender {
    DHDianpingListController *vc = [[DHDianpingListController alloc] init];
    vc.brandID = self.brandID;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.itemArr.count == 0) {
        return 1;
    }else{
        return self.itemArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.itemArr.count == 0) {
        return 40;
    }else{
        return 95;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.itemArr.count > 0) {
        DHHomeComments_Cell *cell = [tableView dequeueReusableCellWithIdentifier:kDHHomeComments_Cell forIndexPath:indexPath];
        NSDictionary *dic = self.itemArr[indexPath.row];
        [cell.imgView dh_setImageURLString:[dic objectForKey:@"cover"]];
        [cell.headImageView dh_setImageURLString:[[dic objectForKey:@"owner"] objectForKey:@"photo"]];
        cell.titleLabel.text = [[dic objectForKey:@"owner"] objectForKey:@"nickname"];
        cell.nameLabel.text = [[dic objectForKey:@"owner"] objectForKey:@"title"];
        cell.summaryLabel.text = [dic objectForKey:@"summary"];
        return cell;
    }else{
        DHArticleNO_CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHArticleNO_CommentCell forIndexPath:indexPath];
        cell.infoLabel.text = @"暂无点评";
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.itemArr[indexPath.row];
    DHNewsDetailController *vc = [[DHNewsDetailController alloc] initWithArticleID:[dic objectForKey:@"_id"]];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
